$(document).ready(function () {
        	$("#confirmMsg").html("<font color=red><b>注:添加用户时默认密码为8个8,需要用户自行登录修改信息！</b></font>");
        	$("select[name='companyName']").change(function(){
        	     var curVal = $(this).val();
        	     if(curVal.indexOf("@")>-1){
        	    	 curVal = curVal.substring(0,curVal.indexOf("@"));
        	     }
        	     loadDepartment(curVal);
        	});
        	
        	$("select[name='deptName']").change(function(){
        	     var curVal = $(this).val();
        	     if(curVal.indexOf("@")>-1){
        	    	 curVal = curVal.substring(0,curVal.indexOf("@"));
        	     }
        	     loadPosition(curVal);
        	});
        	
        	//加载表单数据
        	var id = new Number($("#editAdminForm input[name='id']").val());
            loadFormData(id);
        	var rules = {}
        	var messages = {}
        	if(id>0){
        		rules = {
                	userName:{
                		required:true,
                		minlength:5,
                	},
                	realName:{
                		required:true
                	}
                };
        		
        		messages = {
                	userName:{
                		required:'请输入用户名',
                		minlength:'用户名不能少于5位数',
                	},
	            	workTel:"请输入工作电话",
	            	mobileTel:"请输入手机号码",
	            	companyUUID:"请选择你的",
	            	groupName:"请选择该用户所属角色",
	            	realName:"请输入真实姓名",
	            	positionName:"请选择你的职位信息"
                };
        	}else{
				rules = {
					userName : {
						required : true,
						minlength : 5,
						remote : getBasePath()+ "adminUser/checkUserName.do"
					},
					email : {
						required : true,
						email : true
					}
				};
				messages = {
					userName : {
						required : '请输入用户名',
						minlength : '用户名不能少于5位数',
						remote : '你输入的用户名已存在！'
					},
					workTel : "请输入工作电话",
					mobileTel : "请输入手机号码",
					realName : "请输入真实姓名"
				}
        	}
        	
        	var validateForm = $("#editAdminForm #dataForm").validate({
                debug:true,
                rules:rules,
                messages:messages
            });
        	
        	$("#editAdminForm input[name='close']").click(function () {
        		 $("#editDataFormArea").window("close");
        	})
        	
            //提交数据到服务器
            $("#editAdminForm input[name='submitData']").click(function () {
            	//提交表单进行验证
            	$("#editAdminForm #dataForm").submit();
                var validateFlag = validateForm.form();
                var actionUrl = $("#editAdminForm #dataForm").attr("action");
            	var formData  = $("#editAdminForm #dataForm").form2Json();
            	if(validateFlag){
            		$("#editAdminForm #dataForm").form('submit',{
            			//当提交时
            			onSubmit:function(param){
            			},
            			success:function(data){
            				// change the JSON string to javascript object 
            				var data = eval('(' + data + ')');
            				if(data.code=="00"){
              				  $.messager.confirm('操作确认',data.msg+',请确认是否继续操作？',function(flag){
              					  if(flag){
              						  $("#editDataFormArea").window("close");
              						  tableObject.datagrid('reload');
              					  }
              				  });
              			    }else{
              				   $.messager.alert('操作提示',data.msg,'error');
              			    }
            			}
            		});
            	}
            });
        });
        
        //加载单位列表
        function loadCompany(){
        	$.post(getBasePath()+"company/getComboboxData.do",{},function(jsonList) {
				var listhtml = "<option value=''>请选择</option>";
				var curCompanyName = $("select[name='companyName']").attr("curV");
				$.each(jsonList, function(i, company) {
				    if(curCompanyName==company.companyName){
				        listhtml = listhtml + "<option selected value='"+company.id+"@"+company.companyName+"'>"+company.companyName+ "</option>";
				        loadDepartment(company.id);
				    }else{
				        listhtml = listhtml + "<option value='"+company.id+"@"+company.companyName+"'>"+company.companyName+ "</option>";
				    }
				});
				$("select[name='companyName']").html(listhtml);
		   },"json");
        }
        
        //加载部门信息
        function loadDepartment(companyUUID){
        	$.post(getBasePath()+"department/getDeptListByCompanyUUID.do",{companyUUID:companyUUID},function(jsonList) {
					var listhtml = "<option value=''>请选择</option>";
					var curDeptName = $("select[name='deptName']").attr("curV");
					$.each(jsonList, function(i, dept) {
					    if(curDeptName==dept.departmentName){
					        listhtml = listhtml + "<option selected value='"+dept.uuid+"@"+dept.departmentName+"'>"+dept.departmentName+ "</option>";
					        loadPosition(dept.id)
					    }else{
					        listhtml = listhtml + "<option value='"+dept.uuid+"@"+dept.departmentName+"'>"+dept.departmentName+ "</option>";
					    }
					});
					$("select[name='deptName']").html(listhtml);
		    },"json");
        }
        
        //加载职位信息
        function loadPosition(deptUUID){
        	$.post(getBasePath()+"position/loadPositionByDeptId.do",{deptUUID:deptUUID},function(jsonList) {
				var listhtml = "<option value=''>请选择</option>";
				var curPositionName = $("select[name='positionName']").attr("curV");
				$.each(jsonList, function(i, bean) {
				    if(curPositionName==bean.positionName){
				        listhtml +=  "<option selected value='"+bean.uuid+"@"+bean.positionName+"'>"+bean.positionName+ "</option>";
				    }else{
				        listhtml +=  "<option value='"+bean.uuid+"@"+bean.positionName+"'>"+bean.positionName+ "</option>";
				    }
				});
				$("select[name='positionName']").html(listhtml);
		    },"json");
        }
        
        //加载角色
        function loadGroup(){
        	$.post(getBasePath()+"group/getComboboxData.do",{},function(jsonList) {
				var listhtml = "<option value=''>请选择</option>";
				var curGroupName = $("select[name='groupName']").attr("curV");
				$.each(jsonList, function(i, group) {
				    if(curGroupName==group.groupName){
				        listhtml = listhtml + "<option selected value='"+group.id+"@"+group.groupName+"'>"+group.groupName+ "</option>";
				    }else{
				        listhtml = listhtml + "<option value='"+group.id+"@"+group.groupName+"'>"+group.groupName+ "</option>";
				    }
				});
				$("select[name='groupName']").html(listhtml);
		   },"json");
        }
        
        //通过表单加载数据
        function loadFormData(id){
        	if(id>0){
        		$.post(getBasePath()+"/adminUser/js/getAdminDetail.do",{id:id},function(jsonData){
            		$("#editAdminForm #dataForm").form("load",jsonData.data);
            		console.log("companyUUID:"+jsonData.data.companyUUID);
            		$("select[name='groupName']").attr("curV",jsonData.data.groupName);
            		$("select[name='deptName']").attr("curV",jsonData.data.deptName);
            		$("select[name='positionName']").attr("curV",jsonData.data.positionName);
            		loadDepartment(jsonData.data.companyUUID);
            		loadGroup();
      		    },"json");
        	}else{
        		loadCompany();
        		loadGroup();
        		loadDepartment();
        	}
        }